import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testnamespace:

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/1_get_namespace_list.yaml'))
    def test_get_namespace_list(self, namespaceinfo, get_env_info):
        # 获取namespace列表接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url']
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/2_create_namespace.yaml'))
    def test_create_namespace(self, namespaceinfo, get_env_info):
        # 创建namespace接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url'].replace('{namespace}','mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/3_get_specific_namespace.yaml'))
    def test_get_specific_namespace(self, namespaceinfo, get_env_info):
        # 获取指定namespace接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url'].replace('{namespace}','mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in namespaceinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/4_update_namespace.yaml'))
    def test_update_namespace(self, namespaceinfo, get_env_info):
        # 更新指定namespace接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url'].replace('{namespace}','mynamespace')
        method = namespaceinfo['request']['method']
        namespaceinfo['request']['data'] = globals()['updatereqbody']
        data = namespaceinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/5_update_section_namespace.yaml'))
    def test_update_section_namespace(self, namespaceinfo, get_env_info):
        # 部分更新指定namespace的接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url'].replace('{namespace}','mynamespace')
        method = namespaceinfo['request']['method']
        data = namespaceinfo['request']['data']
        header = namespaceinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('namespaceinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/namespace/6_delete_specific_namespace.yaml'))
    def test_delete_specific_namespace(self, namespaceinfo, get_env_info):
        # 删除namespace接口
        baseUrl = get_env_info
        url = baseUrl + namespaceinfo['request']['url'].replace('{namespace}','mynamespace')
        method = namespaceinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == namespaceinfo['validate'][0]['equals']['status_code']